package cn.hn.tx.rebot.task.job;

import java.util.ArrayList;
import java.util.List;

import cn.hn.comm.Tools;
import cn.hn.comm.exception.IExceptionService;
import cn.hn.tx.rebot.blog.model.Blog;
import cn.hn.tx.rebot.blog.model.Comment;
import cn.hn.tx.rebot.blog.service.IRebotBlogService;
import cn.hn.tx.rebot.task.model.TaskExeLog;
import cn.hn.tx.uchblog.model.UchomeBlog;
import cn.hn.tx.uchblog.model.UchomeComment;
import cn.hn.tx.uchblog.service.IUchomeBlogService;
import cn.hn.tx.usermanager.model.PublishBlogUser;
import cn.hn.tx.usermanager.service.IUserManagerService;

public class TaskJob_PublishBlogComment extends ITaskJob {

	private IUserManagerService userManagerService;
	private IRebotBlogService rebotBlogService;
	private IUchomeBlogService uchomeBlogService;

	// 异常记录
	private IExceptionService exceptionService;

	@Override
	void executeJob() {
		System.out.println("开始:" + getTaskName());
		// 任务执行记录日志
		TaskExeLog taskExeLog = new TaskExeLog();
		taskExeLog.setStartTime(Tools.getCurDate(null));
		taskExeLog.setTaskName(this.getTaskName());
		taskExeLog.setType(type().getType());
		String taskLogMemo = null;
		
		int iBlogCount=0;
		int iBlogComment=0;

		try {
			// 得到需要发布评论的日志
			List<Blog> listBlog = rebotBlogService
					.queryRebotWriteBlogByStatus_Comment(Blog.STATUS_UPDATE_YES);
			// 逐个处理日志
			for (Blog blog : listBlog) {
				if(blog.getBlogId()==null)
				{
					continue;
				}
				// 得到该日志需要发布的评论
				List<Comment> listComment = rebotBlogService
						.queryListRebotComment(blog.getId(),
								Comment.STATUS_PUBLIC_NO);
				// 如果有评论可以发
				if (null != listComment && listComment.size() > 0) {

					// 找到UCHOME_BLOG
					UchomeBlog uchomeBlog = uchomeBlogService
							.queryUchomeBlogByBid(blog.getBlogId());

					// 得到例外的用户
					String except = String.valueOf(uchomeBlog.getUid());
					List<String> listExcept = new ArrayList<String>();
					listExcept.add(except);

					// 找评论者
					List<PublishBlogUser> listPubishUser = userManagerService
							.queryPublishBlogUserListByExcept(
									PublishBlogUser.EN_ABLE_TRUE,
									PublishBlogUser.TYPE_COMMENT, listExcept);

					// // 只发一个评论
					// // 评论
					// Comment comment = listComment.get(0);
					// // 评论者
					// PublishBlogUser publishUser = listPubishUser.get(0);
					// // 创建UCHOME评论
					// UchomeComment uchComment = new UchomeComment();
					// uchComment.setAuthor(publishUser.getUserName());
					// uchComment.setAuthorid(publishUser.getUid());
					// uchComment.setDateline(System.currentTimeMillis() /
					// 1000);
					// uchComment.setId(uchomeBlog.getBlogid());
					// uchComment.setIdtype("blogid");
					// uchComment.setIp("127.0.0.1");
					// uchComment.setMagicflicker(0);
					// uchComment.setMessage(comment.getComment());
					// uchComment.setUid(uchomeBlog.getUid());
					// // 发布评论
					// uchomeBlogService.insertUchomeComment(uchComment);
					// // 保存已经发布的评论状态
					// comment.setPublish_time(Tools.getCurDate(null));
					// comment.setStatus(Comment.STATUS_PUBLIC_YES);
					// comment.setUid(publishUser.getUid());
					// rebotBlogService.updateRebotComment(comment);

					// 逐个发布评论
					Long lDateTime=System.currentTimeMillis()/1000;
					Long lDateTimeDis=lDateTime-uchomeBlog.getDateline();
					lDateTimeDis=lDateTimeDis/listComment.size();
					for (int i = 0; i < listComment.size(); i++) {
						// 评论
						Comment comment = listComment.get(i);
						// 评论者
						PublishBlogUser publishUser = listPubishUser.get(i
								% listPubishUser.size());
						// 创建UCHOME评论
						UchomeComment uchComment = new UchomeComment();
						uchComment.setAuthor(publishUser.getUserName());
						uchComment.setAuthorid(publishUser.getUid());
						
						lDateTime+=lDateTimeDis;
						uchComment.setDateline(lDateTime);
						 uchComment.setId(uchomeBlog.getBlogid());
						 uchComment.setIdtype("blogid");
						 uchComment.setIp("127.0.0.1");
						 uchComment.setMagicflicker(0);
						 uchComment.setMessage(comment.getComment());
						 uchComment.setUid(uchomeBlog.getUid());
						 // 发布评论
						 uchomeBlogService.insertUchomeComment(uchComment);
						 // 保存已经发布的评论状态
						 comment.setPublish_time(Tools.getCurDate(null));
						 comment.setStatus(Comment.STATUS_PUBLIC_YES);
						 comment.setUid(publishUser.getUid());
						 rebotBlogService.updateRebotComment(comment);						
					}// end for(逐个发布评论)

					// 保存日志已经发布的评论数量
					blog.setPublish_comment(blog.getPublish_comment() + listComment.size());
					rebotBlogService.updateRebotWriteBlog(blog);
					iBlogCount++;
					iBlogComment+= listComment.size();
					
					//更改UCHOME_BLOG的回复数量
					uchomeBlog.setReplynum(uchomeBlog.getReplynum()+listComment.size());
					uchomeBlogService.updateUchomeBlog(uchomeBlog);
					
				}
			}// end for(逐个处理日志)
			taskLogMemo="发布评论日志数="+iBlogCount+";发布相应评论数="+iBlogComment;
		} catch (Exception e) {
			taskLogMemo = e.toString();
			exceptionService.insertException(this.getClass().getName(),
					"executeJob()", e.toString());
		}

		taskExeLog.setMemo(taskLogMemo);
		taskExeLog.setEndTime(Tools.getCurDate(null));
		// 记录任务执行日志
		getTaskService().insertTaskExeLog(taskExeLog);
		System.out.println("结束:" + getTaskName());

	}

	@Override
	public TaskType type() {
		return TaskType.PublishBlogComment;
	}

	public IRebotBlogService getRebotBlogService() {
		return rebotBlogService;
	}

	public void setRebotBlogService(IRebotBlogService rebotBlogService) {
		this.rebotBlogService = rebotBlogService;
	}

	public IUchomeBlogService getUchomeBlogService() {
		return uchomeBlogService;
	}

	public void setUchomeBlogService(IUchomeBlogService uchomeBlogService) {
		this.uchomeBlogService = uchomeBlogService;
	}

	public IUserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(IUserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public IExceptionService getExceptionService() {
		return exceptionService;
	}

	public void setExceptionService(IExceptionService exceptionService) {
		this.exceptionService = exceptionService;
	}

}
